const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        id: '',
        plan: '',
        radio: '',
        title:'',
        content:'',
        planStartTime: '',
        planEndTime: '',
        date: '',
        planStartTimeShow: false,
        planEndTimeShow: false,
        planStartTimeAfter:'',
        planEndTimeAfter:''
    },

    titleInput(e){
        this.setData({
            title: e.detail
        })
    },
    contentInput(e){
        this.setData({
            content: e.detail
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            id: options.id,
        });
        this.queryPlanById();
    },

    onChange(event) {
        this.setData({
            radio: event.detail,
        });
    },

    onClick(event) {
        const { name } = event.currentTarget.dataset;
        this.setData({
            radio: name,
        });
    },


    queryPlanById(){
        wx.request({
            url: app.globalData.baseUrl + '/plan/queryPlanById?id='+this.data.id,
            method: 'GET',
            header: {
                "token": wx.getStorageSync('token')
            },
            success: res => {
                if (res.data.code === 200) {
                    this.setData({
                        plan : res.data.result,
                        radio: res.data.result.status,
                        title: res.data.result.title,
                        content: res.data.result.content,
                        planStartTime: res.data.result.planStartTime,
                        planEndTime: res.data.result.planEndTime
                    })
                }
            }
        })
    },

    saveEdit(){
        wx.request({
            url: app.globalData.baseUrl + '/plan/editPlan',
            method: 'POST',
            header: {
                "Content-Type": "application/json;charset=utf-8",
                "token": wx.getStorageSync('token')
            },
            data: {
                id: this.data.id,
                title: this.data.title,
                content: this.data.content,
                status: this.data.radio
            },
            success: res => {
                if (res.data.code === 200) {
                    wx.showToast({
                        title: '编辑成功',
                        icon: 'none'
                    });
                    wx.switchTab({
                        url: '../agency/agency'
                    })
                }
            }
        })
    },

    onStartDisplay() {
        this.setData({ planStartTimeShow: true });
    },
    onEndDisplay() {
        this.setData({ planEndTimeShow: true });
    },
    onStartClose() {
        this.setData({ planStartTimeShow: false });
    },
    onEndClose() {
        this.setData({ planEndTimeShow: false });
    },

    formatDate(date) {
        date = new Date(date);
        return `${date.getMonth() + 1}/${date.getDate()}`;
    },
    onStartConfirm(event) {
        this.setData({
            planStartTimeAfter: event.detail,
            planStartTimeShow: false,
            planStartTime: this.formatDate(event.detail),
        });
    },

    onEndConfirm(event) {
        this.setData({
            planEndTimeAfter: event.detail,
            planEndTimeShow: false,
            planEndTime: this.formatDate(event.detail),
        });
    },

    onInput(event) {
        this.setData({
            currentDate: event.detail,
        });
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})
